{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# exportByFeat(img, fc, prop, folder, name, scale, dataType, **kwargs):\n",
    "Export an image clipped by features (Polygons). You can use the same arguments as the original function ee.batch.export.image.toDrive\n",
    "\n",
    "**Parameters**\n",
    "\n",
    "- img: image to clip\n",
    "- fc: feature collection\n",
    "- prop: name of the property of the features to paste in the image\n",
    "- folder: same as ee.Export\n",
    "- name: name of the resulting image. If `None` uses image's ID\n",
    "- scale: same as ee.Export. Default to 1000\n",
    "- dataType: as downloaded images **must** have the same data type in all\n",
    "    bands, you have to set it here. Can be one of: \"float\", \"double\", \"int\",\n",
    "    \"Uint8\", \"Int8\" or a casting function like *ee.Image.toFloat*\n",
    "- kwargs: keyword arguments that will be passed to ee.batch.export.image.toDrive\n",
    "\n",
    "Return a list of all tasks (for further processing/checking)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "ee.Initialize()\n",
    "from geetools import batch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FeatureCollection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "p1 = ee.Geometry.Point([-71,-42])\n",
    "p2 = ee.Geometry.Point([-71,-43])\n",
    "p3 = ee.Geometry.Point([-71,-44])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "feat1 = ee.Feature(p1.buffer(1000), {'site': 1})\n",
    "feat2 = ee.Feature(p2.buffer(1000), {'site': 2})\n",
    "feat3 = ee.Feature(p3.buffer(1000), {'site': 3})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "fc = ee.FeatureCollection([feat1, feat2, feat3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "collection = ee.ImageCollection('COPERNICUS/S2').filterBounds(fc.geometry())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = collection.mosaic()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Execute"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "exporting test_site_1\n",
      "exporting test_site_2\n",
      "exporting test_site_3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[<Task EXPORT_IMAGE: test_site_1 (UNSUBMITTED)>,\n",
       " <Task EXPORT_IMAGE: test_site_2 (UNSUBMITTED)>,\n",
       " <Task EXPORT_IMAGE: test_site_3 (UNSUBMITTED)>]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch.Image.toDriveByFeat(image, collection=fc, property='site', folder='tools_exportbyfeat', name='test', scale=10, dataType='float')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
